package com.erji.common.cos;


import com.erji.common.cos.config.TencentCOSproperties;
import com.erji.common.cos.domain.FileObject;
import com.erji.common.utils.EncryptUtils;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.ObjectMetadata;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.InputStream;

@Component
public class FileManagerImpl implements FileManager {
    public static final int NOT_EXPIRED = -1;
    private static final Logger logger = LoggerFactory.getLogger(FileManagerImpl.class);

    private TencentCOSproperties tencentCOSproperties;
    private COSClient cosClient;

    public FileManagerImpl(TencentCOSproperties tencentCOSproperties, COSClient cosClient) {
        this.tencentCOSproperties = tencentCOSproperties;
        this.cosClient = cosClient;
    }

    public FileObject upload(String objKey, InputStream inputStream) {
        return this.upload(this.getFileSimpleName(objKey), objKey, inputStream, -1);
    }

    public FileObject upload(String objKey, InputStream inputStream, ObjectMetadata metadata) {
        return this.upload(this.getFileSimpleName(objKey), objKey, inputStream, metadata, -1);
    }

    public FileObject upload(String fileName, String objKey, InputStream inputStream, int expiredMilliseconds) {
        return this.upload(fileName, objKey, inputStream, (ObjectMetadata) null, -1);
    }

    public FileObject upload(String fileName, String objKey, InputStream inputStream, ObjectMetadata metadata, int expiredMilliseconds) {
        Object var6 = null;

        try {
            this.cosClient.putObject(this.tencentCOSproperties.getBucketName(), objKey, inputStream, metadata);
            if (StringUtils.isBlank(fileName)) {
                fileName = this.getFileSimpleName(objKey);
            }

            return new FileObject(EncryptUtils.encodeBase64String(objKey), fileName, objKey, this.generateUrl(objKey, expiredMilliseconds));
        } catch (Exception var8) {
            logger.error("Oss uploaded failed: " + var8.getMessage());
            logger.error(var8.getMessage(), var8);
            return new FileObject();
        }
    }

    public boolean checkExist(String objectKey) {
        return this.cosClient.doesObjectExist(tencentCOSproperties.getBucketName(), objectKey);
    }

    public void remove(String objKey) {
        this.cosClient.deleteObject(tencentCOSproperties.getBucketName(), objKey);
    }

    public ObjectMetadata getObjectMetadata(String url) {
        return this.cosClient.getObjectMetadata(tencentCOSproperties.getBucketName(), url);
    }

    public String generateUrl(String objectKey) {
        return this.generateUrl(objectKey, -1);
    }

    public String generateUrl(String objectKey, int expiredMilliseconds) {
        return expiredMilliseconds > -1 ? null : tencentCOSproperties.getRootSrc() + "/" + objectKey;
    }

    public void rename(FileObject file, String targetObjKey, int expiredMilliseconds) {
        this.cosClient.copyObject(tencentCOSproperties.getBucketName(), file.getObjKey(), tencentCOSproperties.getBucketName(), targetObjKey);
        this.remove(file.getObjKey());
        file.setObjKey(targetObjKey);
        file.setUrl(this.generateUrl(targetObjKey, expiredMilliseconds));
    }

    public void rename(FileObject f, String targetObjKey) {
        this.rename(f, targetObjKey, -1);
    }

    public void updateFolder(FileObject file, String sourceFolder, String targetFolder) {
        this.updateFolder(file, sourceFolder, targetFolder, -1);
    }

    public void updateFolder(FileObject file, String sourceFolder, String targetFolder, int expiredMilliseconds) {
        if (!StringUtils.isBlank(file.getObjKey()) && !StringUtils.equals(sourceFolder, targetFolder)) {
            String newObjectKey = StringUtils.replace(file.getObjKey(), sourceFolder, targetFolder);
            this.rename(file, newObjectKey, expiredMilliseconds);
        }
    }

    private String getFileSimpleName(String oriUrl) {
        return StringUtils.indexOf(oriUrl, "/") > 0 ? StringUtils.substring(oriUrl, StringUtils.indexOf(oriUrl, "/") + 1) : oriUrl;
    }
}
